<?php

require_once (dirname(__file__) . '/../Option.php');

/**
 * Options API
 * 
 * @author rutz
 */
class Options
{

  protected static $db;

  public static function save($name, $value = null)
  {
    $option = self::find($name);
    
    if (empty($option)) {
      $option = new Option();
      $option->name = $name;
      $option->value = $value;
      self::getDAO()->save($option);
    }
  }

  public static function update($name, $value = null)
  {
    $option = self::find($name);
    
    if (empty($option)) {
      self::save($name, $value);
    }
    else {
      $option->name = $name;
      $option->value = $value;
      self::getDAO()->save($option);
    }
  }

  public static function value($name, $default = null, $filter = null)
  {
    $option = self::find($name);
    
    if (empty($option)) {
      $option = new Option();
    }
    
    return $option->get('value', $default, $filter);
  }

  public static function find($name)
  {
    $option = self::getDAO()->from('Option')->where('{Option.option_name} = ?', ( array ) $name)->findOne();
    return $option;
  }

  protected static function getDAO()
  {
    if (empty(self::$db)) {
      self::$db = OutletDAO::getInstance()->addEntity('Option');
    }
    return self::$db;
  }

}

?>